package com.pktechan.manage.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.StatusFlow;
import com.pktechan.manage.service.StatusFlowService;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.vo.StatusFlowVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 状态流水相关功能
 * @author: s.zhang
 * @create: 2021-03-09 15:02
 **/
@Controller
@RequestMapping("statusFlowController")
public class StatusFlowController {
    private static final Logger logger = LoggerFactory.getLogger(StatusFlowController.class);

    @Autowired
    private StatusFlowService statusFlowService;

    /**
     * 根据id查询状态流水表
     *
     * @param flowId
     * @param response
     */
    @RequestMapping(value = "getStatusFlowById", method = RequestMethod.POST)
    public void getStatusFlowById(@RequestParam Integer flowId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + flowId + "查询状态流水");
            StatusFlow statusFlow = statusFlowService.getById(flowId);
            HtmlUtil.writerSuccessJson(response, statusFlow);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 查询状态流水表列表
     *
     * @param statusFlowVo
     * @param response
     */
    @RequestMapping(value = "getStatusFlowVoList", method = RequestMethod.POST)
    public void getStatusFlowVoList(@RequestBody StatusFlowVo statusFlowVo, HttpServletResponse response) {
        try {
            logger.info("查询状态流水列表");
            PageHelper.startPage(statusFlowVo.getPageNo(), statusFlowVo.getPageSize());
            List<StatusFlowVo> list = statusFlowService.getStatusFlowVoList(statusFlowVo);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到状态流水表
     *
     * @param statusFlow
     * @param response
     */
    @RequestMapping(value = "saveStatusFlow", method = RequestMethod.POST)
    public void saveStatusFlow(@RequestBody StatusFlow statusFlow, HttpServletResponse response) {
        try {
            logger.info("添加状态流水");
            statusFlowService.save(statusFlow);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id状态流水表
     *
     * @param statusFlow
     * @param response
     */
    @RequestMapping(value = "updateStatusFlowById", method = RequestMethod.POST)
    public void updateStatusFlowById(@RequestBody StatusFlow statusFlow, HttpServletResponse response) {
        try {
            logger.info("根据id=" + statusFlow.getId() + "状态流水");
            statusFlowService.update(statusFlow);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id删除状态流水表
     *
     * @param flowId
     * @param response
     */
    @RequestMapping(value = "deleteStatusFlowById", method = RequestMethod.POST)
    public void deleteStatusFlowById(@RequestParam Integer flowId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + flowId + "删除状态流水");
            statusFlowService.deleteById(flowId);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 打开状态流水列表页面
     * @return
     */
    @RequestMapping("toStatusFlowVoList")
    public String toStatusFlowVoList() {
        return "statusFlow/statusFlowVoList";
    }
}
